package com.hc.fdkforwarders.service;

import com.hc.fdkforwarders.common.page.PageContent;
import com.hc.fdkforwarders.common.page.PageContentContainer;
import com.hc.fdkforwarders.dao.GoodsFindDao;
import com.hc.fdkforwarders.vo.GoodsFindQueryVO;
import com.hc.fdkforwarders.vo.GoodsFindVO;
import com.hc.fdkforwarders.vo.SkuVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ForwarderGoodsService {

    @Resource
    private GoodsFindDao goodsFindDao;

    public PageContent<GoodsFindVO> queryGoods(GoodsFindQueryVO goodsFindQuery) {

        PageContent<GoodsFindVO> res = new PageContent<>();

        ArrayList<GoodsFindVO> goodsFindVOS = new ArrayList<>();

        PageContentContainer<SkuVO> skuList = goodsFindDao.getGoodsList(goodsFindQuery);

        List<SkuVO> list = skuList.getList();

        Map<Long, List<SkuVO>> spuId2SkuListMap = list.stream().collect(Collectors.groupingBy(SkuVO::getSpuId));


        spuId2SkuListMap.forEach((spuId, skus) -> {
            GoodsFindVO goodsFindVO = new GoodsFindVO();

            SkuVO skuVO = skus.get(0);

            goodsFindVO.setSpuGoodsNo(skuVO.getSpuGoodsNo());
            goodsFindVO.setBizFullName(skuVO.getBizFullName());
            goodsFindVO.setMainImgUrl(skuVO.getMainImgUrl());

            goodsFindVO.setSkuList(skus);

            goodsFindVOS.add(goodsFindVO);

        });

        res.setList(goodsFindVOS);

        res.setTotal(skuList.getTotal());

        return res;

    }
}
